package operation;

import book.Book;
import book.Bookshelf;

import java.util.Scanner;
import java.util.concurrent.locks.AbstractOwnableSynchronizer;

public class DelOperation implements IOperation{
    public void work(Bookshelf bookshelf) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书名：");
        String name = scanner.nextLine();
        int cur = bookshelf.getUsedSize();
        for (int i = 0; i < cur; i++) {
            Book book = bookshelf.getBook(i);
            if (book.getName().equals(name)) {
                //删除
                //后面的向前覆盖
                int j = i;
                for ( ; j < bookshelf.getUsedSize() - 1; j++) {
                    bookshelf.setBooks(j,bookshelf.getBook(j + 1));
                }
                System.out.println("删除成功");
                //最后把 j 位置置空
                bookshelf.setBooks(j,null);
                bookshelf.setUsedSize(cur - 1);
                return;
            }
        }

        System.out.println("没有这本书");
    }
}